package com.kmxd.ams.core.common.enums;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.kmxd.ams.core.common.constant.ErrorCode;
import com.kmxd.ams.core.exception.BizException;
import java.util.Arrays;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 通知类型
 *
 * @author nickbi
 */
@Getter
@AllArgsConstructor
public enum MessageEventEnum {

  /** 系统通知 */
  SYSTEM(1, "系统通知"),
  /** 业务通知 */
  BIZ(2, "业务通知"),
  AUDIT(3,"审核"),
  BROADCAST(4, "系统广播"),
  ;
  private Integer type;
  private String desc;

  public static MessageEventEnum convert(Integer type) {
    return Arrays.stream(values())
        .filter(x -> x.type.equals(type))
        .findAny()
        .orElseThrow(() -> BizException.instance(ErrorCode.NOT_FOUND, "未找消息类型：" + type));
  }

  public static MessageEventEnum convert(String desc) {
    return Arrays.stream(values()).filter(x -> x.desc.equals(desc)).findAny().orElse(SYSTEM);
  }

  @JsonCreator
  public static MessageEventEnum getValue(Integer type) {
    return Arrays.stream(values()).filter(x -> Objects.equals(x.type, type)).findAny().orElse(null);
  }
}
